//
//  LiveAllTypeViewController.m
//  PrintPort
//
//  Created by hy002 on 2017/12/29.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "LiveAllTypeViewController.h"
#import "LiveGoodsDetailViewController.h"
#import "ProdTypeModel.h"
#import "ProSmallTypeModel.h"
#import "LifeGoodsModel.h"

static NSString *cellPath = @"LiveAllTypeCell";
static NSString * CellIdentifier = @"LivesmallCell";
static NSString * CellIHerder = @"LiveHerderCell";
@interface LiveAllTypeViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>
{
    CGFloat width;
}
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, assign)NSInteger leftType;
@property (nonatomic, strong)NSArray *bigTypeAry;


@end

@implementation LiveAllTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    width = SCREEN_WIDTH-100*displaywidth;
    _leftType = 0;
    [self creatUI];
    [self requestLifeByParentProductTypeId];
}

- (void)creatUI
{
    self.navigationItem.title = @"分类列表";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = BacksColor;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 75*displaywidth, SCREEN_HEIGHT-STA_NAV_HEIGHT) style:UITableViewStylePlain];
    _tableview.backgroundColor = BacksColor;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(87.5*displaywidth, 0, width, SCREEN_HEIGHT-STA_NAV_HEIGHT) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CellIHerder];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.bounces = NO;
    [self.view addSubview:_collectionView];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allTypeAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_allTypeAry.count > 0)
    {
        UIColor *cellBack = [UIColor whiteColor];
        UIColor *nameColor = RGB(77, 77, 77);
        BOOL hide = YES;
        if (indexPath.row == _leftType)
        {
            cellBack = BacksColor;
            nameColor = ThemeColor;
            hide = NO;
        }
        cell.backgroundColor = cellBack;
        ProdTypeModel *typeModel = _allTypeAry[indexPath.row];
        UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(5*displaywidth, 0, 65*displaywidth, 50*displaywidth) Font:13.0 Text:typeModel.typeName Color:nameColor];
        nameLab.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:nameLab];
        UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(0, 49*displaywidth, 75*displaywidth, 1*displaywidth)];
        lineAview.backgroundColor = BacksColor;
        [cell.contentView addSubview:lineAview];
        UIView *closeBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5*displaywidth, 50*displaywidth)];
        closeBackView.backgroundColor = ThemeColor;
        closeBackView.hidden = hide;
        [cell.contentView addSubview:closeBackView];
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _leftType = indexPath.row;
    [tableView reloadData];
    [self requestLifeByParentProductTypeId];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50*displaywidth;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _bigTypeAry.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_bigTypeAry.count > 0)
    {
        ProSmallTypeModel *model = _bigTypeAry[section];
        NSArray *smallAry = model.productList;
        return smallAry.count;
    }
    return 0;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    cell.backgroundColor = [UIColor whiteColor];
    if (_bigTypeAry.count > 0)
    {
        ProSmallTypeModel *model = _bigTypeAry[indexPath.section];
        NSArray *smallAry = model.productList;
        LifeGoodsModel *goodsModel = smallAry[indexPath.row];
        CGFloat W = width/3;
        UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake((W-60*displaywidth)/2, 15*displaywidth, 60*displaywidth, 60*displaywidth) ImageName:@"face222"];
        [imgView sd_setImageWithURL:URL(goodsModel.logoImgUrl) placeholderImage:IMAGE(@"face222")];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [cell.contentView addSubview:imgView];
        UILabel *titlab = [ZCControl createLabelWithFrame:CGRectMake(0, 75*displaywidth, W, 35*displaywidth) Font:12.0 Text:goodsModel.productName Color:RGB(77, 77, 77)];
        titlab.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:titlab];
    }
    
    return cell;
}

//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(width/3, 110*displaywidth);
}

//cell的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //cell被电击后移动的动画
    [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];
    ProSmallTypeModel *model = _bigTypeAry[indexPath.section];
    NSArray *smallAry = model.productList;
    LifeGoodsModel *goodsModel = smallAry[indexPath.row];
    LiveGoodsDetailViewController *vc = [[LiveGoodsDetailViewController alloc] init];
    vc.idStr = s_str(goodsModel.ID);
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    CGSize size = {width, 50*displaywidth};
    return size;
    
}
//在表头内添加内容,需要创建一个继承collectionReusableView的类,用法类比tableViewcell
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 初始化表头
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CellIHerder forIndexPath:indexPath];
    if (kind == UICollectionElementKindSectionHeader)
    {
        headerView.backgroundColor = BacksColor;
        for (id subView in headerView.subviews) {
            [subView removeFromSuperview];
        }
        ProSmallTypeModel *model = _bigTypeAry[indexPath.section];
        UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(12.5*displaywidth, 0, width-25*displaywidth, 50*displaywidth) Font:13.0 Text:model.typeName Color:BlaTiColor];
        [headerView addSubview:nameLab];
    }
    
    return headerView;
}

- (void)requestLifeByParentProductTypeId
{
    WEAKSELF;
     ProdTypeModel *typeModel = _allTypeAry[_leftType];
    NSDictionary *dic = @{@"parentProductTypeId":s_str(typeModel.ID)};
    [HYBaseHandler requestLifeByParentProductTypeIdParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            weakSelf.bigTypeAry = [ProSmallTypeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.collectionView reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
